﻿using System.Runtime.InteropServices;

namespace Sage.WindowsHotKey.Native
{

    /// <summary>
    /// 提供 Win32 热键相关 API 的封装
    /// </summary>
    public static partial class Win32HotKeyApi
    {
        private const string User32 = "user32.dll";

        /// <summary>
        /// 热键消息的Windows消息ID
        /// </summary>
        public const int WM_HOTKEY = 0x0312;

        /// <summary>
        /// 修饰键标志位
        /// </summary>
        [Flags]
        public enum ModifierKeys : uint
        {
            /// <summary>
            /// 无修饰键
            /// </summary>
            None = 0x0000,

            /// <summary>
            /// Alt 键
            /// </summary>
            Alt = 0x0001,

            /// <summary>
            /// Ctrl 键
            /// </summary>
            Control = 0x0002,

            /// <summary>
            /// Shift 键
            /// </summary>
            Shift = 0x0004,

            /// <summary>
            /// Windows 键
            /// </summary>
            Win = 0x0008,

            /// <summary>
            /// 禁止重复触发热键（仅在按下时触发一次，直到释放）
            /// </summary>
            NoRepeat = 0x4000
        }

        /// <summary>
        /// RegisterHotKey 函数的包装，用于注册全局热键
        /// </summary>
        [LibraryImport(User32, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static partial bool RegisterHotKey(
            nint hWnd,
            int id,
            uint fsModifiers,
            uint vk);

        /// <summary>
        /// UnregisterHotKey 函数的包装，用于注销全局热键
        /// </summary>
        [LibraryImport(User32, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static partial bool UnregisterHotKey(
            nint hWnd,
            int id);

        /// <summary>
        /// 注册全局热键
        /// </summary>
        /// <param name="windowHandle">窗口句柄</param>
        /// <param name="hotkeyId">热键ID</param>
        /// <param name="modifiers">修饰键</param>
        /// <param name="virtualKeyCode">虚拟键码</param>
        /// <returns>是否注册成功</returns>
        public static bool RegisterGlobalHotKey(nint windowHandle, int hotkeyId, ModifierKeys modifiers, uint virtualKeyCode)
        {
            return RegisterHotKey(windowHandle, hotkeyId, (uint)modifiers, virtualKeyCode);
        }

        /// <summary>
        /// 注销全局热键
        /// </summary>
        /// <param name="windowHandle">窗口句柄</param>
        /// <param name="hotkeyId">热键ID</param>
        /// <returns>是否注销成功</returns>
        public static bool UnregisterGlobalHotKey(nint windowHandle, int hotkeyId)
        {
            return UnregisterHotKey(windowHandle, hotkeyId);
        }

        /// <summary>
        /// 将Windows.Forms.Keys修饰键转换为Win32 ModifierKeys
        /// </summary>
        /// <param name="modifiers">Windows.Forms.Keys 类型的修饰键</param>
        /// <returns>转换后的 ModifierKeys 枚举值</returns>
        public static ModifierKeys ConvertToModifiers(Keys modifiers)
        {
            ModifierKeys keys = ModifierKeys.None;

            if ((modifiers & Keys.Alt) == Keys.Alt)
                keys |= ModifierKeys.Alt;
            if ((modifiers & Keys.Control) == Keys.Control)
                keys |= ModifierKeys.Control;
            if ((modifiers & Keys.Shift) == Keys.Shift)
                keys |= ModifierKeys.Shift;

            return keys;
        }
    }
}
